home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / source / src / basic / _memory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  5.6 KB  |  229 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  _memory.c
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #include <LEDA/basic.h>
  16.  
  17. //------------------------------------------------------------------------------
  18. // Memory Management
  19. //------------------------------------------------------------------------------
  20.  
  21. //const int memory_block_bytes = 4092; // size of block 2^k - 4  bytes
  22. const int memory_block_bytes = 8188; // size of block 2^13 - 4  bytes
  23. const int memory_max_size    = 255;  // maximal size of structures in bytes
  24.  
  25. memory_elem_ptr        memory_free_list[256];     //memory_max_size + 1
  26.  
  27. static long int        memory_total_count[256]; 
  28. static memory_elem_ptr memory_block_list[256];
  29. static int             memory_block_count[256];
  30. static int             memory_initialized = 0;
  31.  
  32.  
  33. static void memory_init()
  34. { for(int i = 0; i <= memory_max_size; i++)
  35.   { memory_free_list[i]   = 0;
  36.     memory_total_count[i] = 0;
  37.     memory_block_list[i]  = 0;
  38.     memory_block_count[i] = 0;
  39.    }
  40.   memory_initialized = 1;
  41.  }
  42.  
  43.  
  44. memory_elem_ptr memory_allocate_block(int b)
  45.   if (memory_initialized == 0) memory_init();
  46.  
  47.   if (b > memory_max_size) 
  48.      error_handler(1,string("allocate_block: size (%d bytes) too big",b));
  49.  
  50.   //allocate new block and slice it into chunks of size b bytes
  51.  
  52.   register memory_elem_ptr p;
  53.   register memory_elem_ptr stop;
  54.  
  55.   register int words = (b + sizeof(void*) - 1)/sizeof(void*);
  56.  
  57.   int bytes = words * sizeof(void*);
  58.   int num   = memory_block_bytes/bytes - 2;
  59.  
  60.   memory_block_count[b]++;
  61.   memory_total_count[b] += num;
  62.  
  63.   if ((p=memory_elem_ptr(malloc(memory_block_bytes))) == 0 )
  64.    { cout << "memory allocation: out of memory\n";
  65.      print_statistics();
  66.      exit(1);
  67.     }
  68.  
  69.   //insert block into list of used blocks
  70.   p->next = memory_block_list[b];
  71.   memory_block_list[b] = p;
  72.   p += words;
  73.  
  74.   memory_free_list[b] = p;
  75.  
  76.   stop = p + (num-1)*words;
  77.   stop->next = 0;
  78.  
  79.   while (p < stop) p = (p->next = p+words);
  80.  
  81.   return memory_free_list[b];
  82. }
  83.  
  84.  
  85. void memory_clear()
  86.   register memory_elem_ptr p;
  87.   int i;
  88.   long used;
  89.  
  90.   for (i=1;i<=memory_max_size;i++)
  91.   { p = memory_free_list[i];
  92.     used = memory_total_count[i];
  93.     while (p) { used--; p = p->next; }
  94.  
  95.     if (used==0)
  96.     { while (memory_block_list[i])
  97.       { p = memory_block_list[i];
  98.         memory_block_list[i] = p->next;
  99.         memory_block_count[i]--;
  100.         free((char*)p);
  101.        }
  102.       memory_free_list[i] = 0;
  103.       memory_total_count[i] = 0;
  104.      }
  105.    }
  106.  
  107. }
  108.  
  109.  
  110. void memory_kill()
  111. { register memory_elem_ptr p;
  112.   int i;
  113.  
  114.   for (i=1;i<=memory_max_size;i++)
  115.   { while (memory_block_list[i])
  116.     { p = memory_block_list[i];
  117.       memory_block_list[i] = p->next;
  118.       free((char*)p);
  119.      }
  120.     memory_free_list[i] = 0;
  121.     memory_total_count[i] = 0;
  122.     memory_block_count[i] = 0;
  123.    }
  124.  
  125.  
  126. }
  127.  
  128. memory_elem_ptr allocate_bytes(int bytes)
  129. { memory_elem_ptr p = memory_free_list[bytes];
  130.   if (p==0) p = memory_allocate_block(bytes);
  131.   memory_free_list[bytes] = p->next;
  132.   return p;
  133. }
  134.  
  135. void deallocate_bytes(void* p, int bytes)
  136. { memory_elem_ptr(p)->next = memory_free_list[bytes];
  137.   memory_free_list[bytes] = memory_elem_ptr(p);
  138.  }
  139.  
  140.  
  141. memory_elem_ptr allocate_bytes_with_check(int bytes)
  142. { memory_elem_ptr p = memory_free_list[bytes];
  143.   if (p==0) p = memory_allocate_block(bytes);
  144.   memory_free_list[bytes] = p->next;
  145.   printf("allocate(%d):   %x\n",bytes,p);
  146.   fflush(stdout);
  147.   return p;
  148. }
  149.  
  150. void deallocate_bytes_with_check(void* p, int bytes)
  151. { printf("deallocate(%d): %x\n",bytes,p);
  152.   fflush(stdout);
  153.   memory_elem_ptr q = memory_free_list[bytes];
  154.   while(q && q != memory_elem_ptr(p)) q = q->next;
  155.   if (q) error_handler(999,string("LEDA memory: pointer %d deleted twice",p));
  156.   memory_elem_ptr(p)->next = memory_free_list[bytes];
  157.   memory_free_list[bytes] = memory_elem_ptr(p);
  158.  }
  159.  
  160.  
  161. memory_elem_ptr allocate_words(int words)
  162. { int bytes = words * sizeof(void*);
  163.   memory_elem_ptr p = memory_free_list[bytes];
  164.   if (p==0) p = memory_allocate_block(bytes);
  165.   memory_free_list[bytes] = p->next;
  166.   return p;
  167. }
  168.  
  169. void deallocate_words(void* p, int words)
  170. { int bytes = words * sizeof(void*);
  171.   memory_elem_ptr(p)->next = memory_free_list[bytes];
  172.   memory_free_list[bytes] = memory_elem_ptr(p);
  173.  }
  174.  
  175.  
  176. int used_memory()
  177. { long int total_bytes=0;
  178.  
  179.   for (int i=1;i<=memory_max_size;i++)
  180.      total_bytes += i*memory_total_count[i];
  181.  
  182.   return total_bytes;
  183.  }
  184.  
  185.  
  186.  
  187. void print_statistics()
  188. {
  189.   cout.flush();
  190.  
  191.   long int total,free,used;
  192.   long int total_bytes=0, free_bytes=0, used_bytes=0, b;
  193.  
  194.  
  195.   printf("\n");
  196.   printf("\t+--------------------------------------------------+\n");
  197.   printf("\t|   size     used     free     blocks     bytes    |\n"); 
  198.   printf("\t+--------------------------------------------------+\n");
  199.  
  200.   for (int i=1;i<=memory_max_size;i++)
  201.     if ((total = memory_total_count[i]) > 0 || memory_free_list[i])
  202.     { printf("\t|   %3d    ",i);
  203.       fflush(stdout);
  204.       memory_elem_ptr p = memory_free_list[i];
  205.       free = 0;
  206.       while (p) { free++; p = p->next; }
  207.       b = total*i; 
  208.       used = total - free;
  209.       free_bytes  += free*i;
  210.       used_bytes  += used*i;
  211.       total_bytes += b;
  212.       printf("%6ld   %6ld    %6d   %8ld    |\n",
  213.               used,free,memory_block_count[i],b);
  214.      }
  215.  
  216.  float kb = float(total_bytes)/1024;
  217.  float sec = used_time();
  218.  
  219.  printf("\t+--------------------------------------------------+\n");
  220.  printf("\t|   time:%6.2f sec              space:%8.2f kb |\n",sec,kb);
  221.  printf("\t+--------------------------------------------------+\n");
  222.  printf("\n");
  223.  fflush(stdout);
  224.  
  225. }
  226.